﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Xna_Shootr
{
    class SpaceShipObject : GameObject, IDrawable, IMoveable
    {
        private static Vector2 MaxVelocity = new Vector2(5.0f, 5.0f);
        //private SimpleBullet[] bullets;
        //private const int MAX_NUM_SIMPLE_BULLETS = 20;
        private Texture2D[] bulletSprites;
        private int weaponIndex = 0;
        private int weaponLevel = 0;

        public SpaceShipObject(Texture2D shipSprite, Texture2D[] bulletSprites, int weaponIndex, Vector2 initPosition)
            : base(shipSprite)
        {
            rotation = MathHelper.PiOver2;
            this.bulletSprites = bulletSprites;
            this.weaponIndex = weaponIndex;
            weaponLevel = 0;
            position = initPosition;
            position.X -= this.center.X;
            position.Y -= this.center.Y;
        }

        private Texture2D currentBulletSprite
        {
            get { return bulletSprites[weaponIndex]; }
        }

        public void Fire()
        {
            new SimpleBullet(currentBulletSprite, position, this.GetUnitVelocity(), true);
        }

        public void WeaponSwitch(int weaponIndex)
        {
            this.weaponIndex = weaponIndex;
            weaponLevel = 0; // reset weapon level
        }

        public void WeaponUpgrade()
        {
            if (weaponLevel < 5)
            {
                weaponLevel++;
            }
        }

        #region IDrawable Members

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(sprite, position, null, Color.White, rotation, center, 1.0f, SpriteEffects.None, 0);
        }

        #endregion

        #region IMoveable Members

        public void Push(Vector2 acceleration)
        {
            this.acceleration = acceleration;
        }

        public void Update(GameTime elapsedTime)
        {
            velocity += acceleration;

            // limit max velocity
            velocity.X = MathHelper.Clamp(velocity.X, -MaxVelocity.X, MaxVelocity.X);
            velocity.Y = MathHelper.Clamp(velocity.Y, -MaxVelocity.Y, MaxVelocity.Y);

            // update position
            position += velocity;
        }

        public bool OutOfBoundCheck(Rectangle rect)
        {
            // handle position out of boundary condition
            if (position.X > rect.Width)
            {
                position.X = 0;
                return true;
            }
            else if (position.X < 0)
            {
                position.X = rect.Width;
                return true;
            }
            if (position.Y > rect.Height)
            {
                position.Y = 0;
                return true;
            }
            else if (position.Y < 0)
            {
                position.Y = rect.Height;
                return true;
            }

            return false;
        }

        #endregion
    }
}
